package cn.lzj.org.controller;

import cn.lzj.basic.util.AjaxResult;
import cn.lzj.basic.util.PageList;
import cn.lzj.org.domain.Employee;
import cn.lzj.org.query.EmployeeQuery;
import cn.lzj.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这是RestFul风格的接口,目的是为了和前端接口对接
 * 部门管理接口
 * 要求前后端传递参数必须要使用json对象
 * 写接口的三要素
 * 1.请求地址----
 * 2.请求参数----
 * 3.返回值----
 * 接口注解有  ①@GetMapping =@RequestMapping(value="/id",method = RequestMethod.GET)
 * ②@Postapping
 * ③@PutMapping
 * ④@PatchMapping
 */
@RestController//这个注解相当于@Controller+@RequestBody
@RequestMapping("/employee")  //路径:local host:8080/employee
public class EmployeeController {
    //注入service层的支持
    @Autowired
    private IEmployeeService employeeService;

    //新增和修改  请求类型为put
    @PutMapping  //路径: localhost:8080/dept/{id},id是占位符,可变
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.util();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util()
                    .setsuccess(false)
                    .setmessage("噢哟，系统报错了，我们正在殴打程序员！！!" + e.getMessage());
        }
    }

    //删除   delete
    //@PathVariable映射 URL 绑定的占位符
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.util();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util()
                    .setsuccess(false)
                    .setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    //获取单条数据的逻辑  请求类型:get
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id) {
        return employeeService.queryById(id);
    }

    //获取所有数据的方法  请求类型:get
    @GetMapping
    public List<Employee> queryAll() {
        return employeeService.queryAll();
    }

    //分页和高级查询  请求类型是post
    @PostMapping("/list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult removeList(@RequestBody List<Long> ids) {
        try {
            employeeService.removeList(ids);
            return AjaxResult.util();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util()
                    .setsuccess(false)
                    .setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId) {
        return employeeService.list(shopId);
    }
}
